#include "stdafx.h"

#include "Modules.h"
#include "GameStartup.h"

#define DEFINE_GLOBAL_VARIABLES
#include "GlobalVars.h"


LRESULT _stdcall MsgProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CLOSE:
		gEnv->pSystem->Shutdown();
		return 0;	//	notify OS that we handled the message

	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE)
			gEnv->pSystem->Shutdown();
		break;

	default:
		if (gEnv->pInput)
			gEnv->pInput->DispatchMessage(msg, wParam, lParam);
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

int _stdcall WinMain(HINSTANCE appInstance, HINSTANCE, char* commandLine, int show)
{
	//	reset gEnv pointer to fail fast if someone call it too early
	gEnv = nullptr;

	//	init game startup params
	CGameStartup starter(appInstance, MsgProc, commandLine);
	gStartupParams = &starter.GetStartupParams();

	//	init system module
	ISystem* pSystem = LoadGameModule<ISystem>(SYSTEM_LIB_NAME);
	if (!pSystem)
		return 0;

	//	process message queue
	MSG msg;
	while (!pSystem->IsShuttingDown())
	{
		while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		pSystem->Update();
	}

	//	fuck the system !!!
	SAFE_DELETE(pSystem);
	
	return 0;
}